<template>
    <basic-container>
        <avue-crud :option="option"
                   :table-loading="loading"
                   :data="data"
                   v-model:page="page"
                   v-model:search="search"
                   :permission="permissionList"
                   :before-open="beforeOpen"
                   v-model="form"
                   ref="crud"
                   @sort-change="sortChange"
                   @row-update="rowUpdate"
                   @row-save="rowSave"
                   @row-del="rowDel"
                   @search-change="searchChange"
                   @search-reset="searchReset"
                   @selection-change="selectionChange"
                   @current-change="currentChange"
                   @size-change="sizeChange"
                   @refresh-change="refreshChange"
                   @on-load="onLoad">
            <template #salesDate-search="{ disabled, size }">
                <!--<div style="display: flex;flex-direction: start;">-->
                    <!--<el-select v-model="query.salesDateRule" placeholder="比较" filterable clearable>-->
                        <!--<el-option label="" value=""></el-option>-->
                        <!--<el-option value=">" label="大于">大于</el-option>-->
                        <!--<el-option value=">=" label="大于等于">大于等于</el-option>-->
                        <!--<el-option value="=" label="等于">等于</el-option>-->
                        <!--<el-option value="!=" label="不等于">不等于</el-option>-->
                        <!--<el-option value="<=" label="小于等于">小于等于</el-option>-->
                        <!--<el-option value="<" label="小于">小于</el-option>-->
                    <!--</el-select>-->
                    <!--<el-date-picker-->
                            <!--size="default"-->
                            <!--v-model="query.salesDateSearch"-->
                            <!--type="date"-->
                            <!--:format="dateFormat"-->
                            <!--:value-format="dateFormat"-->
                            <!--:unlink-panels="true"-->
                            <!--clearable-->
                            <!--placeholder="订单日期"-->
                            <!--:picker-options="pickerOptions"-->
                    <!--&gt;-->
                    <!--</el-date-picker>-->
                <!--</div>-->
                <el-date-picker
                        size="default"
                        style="width:94%"
                        v-model="this.salesDateRange"
                        type="daterange"
                        :format="dateFormat"
                        :value-format="dateFormat"
                        unlink-panels
                        start-placeholder="Start Date"
                        end-placeholder="End Date"
                        clearable
                        placeholder="订单日期"
                        @change="handleDateChange"
                        :picker-options="pickerOptions"
                >
                </el-date-picker>
            </template>

            <template #outDate-search="{ disabled, size }">
                <!--<div style="display: flex;flex-direction: start;">-->
                    <!--<el-select v-model="query.outDateRule" placeholder="比较" filterable clearable>-->
                        <!--<el-option label="" value=""></el-option>-->
                        <!--<el-option value=">" label="大于">大于</el-option>-->
                        <!--<el-option value=">=" label="大于等于">大于等于</el-option>-->
                        <!--<el-option value="=" label="等于">等于</el-option>-->
                        <!--<el-option value="!=" label="不等于">不等于</el-option>-->
                        <!--<el-option value="<=" label="小于等于">小于等于</el-option>-->
                        <!--<el-option value="<" label="小于">小于</el-option>-->
                    <!--</el-select>-->
                    <!--<el-date-picker-->
                            <!--size="default"-->
                            <!--v-model="query.outDateSearch"-->
                            <!--type="date"-->
                            <!--:format="dateFormat"-->
                            <!--:value-format="dateFormat"-->
                            <!--:unlink-panels="true"-->
                            <!--clearable-->
                            <!--placeholder="出仓日期"-->
                            <!--:picker-options="pickerOptions"-->
                    <!--&gt;-->
                    <!--</el-date-picker>-->
                <!--</div>-->
                <el-date-picker
                        size="default"
                        style="width:94%"
                        v-model="this.outDateRange"
                        type="daterange"
                        :format="dateFormat"
                        :value-format="dateFormat"
                        unlink-panels
                        start-placeholder="Start Date"
                        end-placeholder="End Date"
                        clearable
                        placeholder="出仓日期"
                        @change="handleOutDateChange"
                        :picker-options="pickerOptions"
                >
                </el-date-picker>
            </template>
        </avue-crud>
    </basic-container>
</template>

<script>
    import {stockOutDetails, getDetail, add, update, remove} from "@/api/anwise/stock/stock_out_item";
    import {mapGetters} from "vuex";
    import { getToken } from '@/utils/auth';
    import { exportBlob } from '@/api/common';
    import { downloadXls } from '@/utils/util';
    import NProgress from 'nprogress';
    import 'nprogress/nprogress.css';
    import {stockHomeSelect,selectStockHomeDict} from "@/api/anwise/stock_home";

    export default {
        props: {
            aboutItem: {
                type: Object,
                default: {}
            },
        },
        data() {
            return {
                form: {},
                query: {},
                search: {},
                salesDateRange:[],
                outDateRange:[],
                loading: true,
                page: {
                    pageSize: 20,
                    currentPage: 1,
                    total: 0
                },
                selectionList: [],
                dateFormat: 'YYYY-MM-DD',
                pickerOptions: {},
                option: {
                    height:'auto',
                    calcHeight: 30,
                    tip: false,
                    searchShow: false,
                    searchMenuSpan: 6,
                    border: true,
                    index: true,
                    selection: false,
                    reserveSelection:true,
                    labelWidth: 150,
                    indexWidth:100,
                    menuWidth: 180,
                    addBtn: false,
                    editBtn: false,
                    viewBtn: false,
                    delBtn: false,
                    stripe: true,
                    searchIndex: 3,
                    searchIcon: false,
                    searchLabelWidth:120,
                    searchEnter:true,
                    menu:false,
                    column: [
                        {
                            label: "出仓日期",
                            prop: "outDate",
                            type: "input",
                            search: true,
                            overHidden: true,
                            sortable: true,
                            width:140,
                            rules: [{
                                required: true,
                                message: "请输入出仓日期",
                                trigger: "blur"
                            }],
                        },
                        {
                            label: "出仓单号",
                            prop: "idNo",
                            type: "input",
                            fixed: true,
                            search: true,
                            overHidden: true,
                            sortable: true,
                            width:180,
                            rules: [{
                                required: true,
                                message: "请输入出仓单号",
                                trigger: "blur"
                            }],
                        },
                        {
                            label: "出仓项",
                            prop: "itemNo",
                            type: "number",
                            precision:0,
                            min:1,
                            controls:false,
                            search: true,
                            overHidden: true,
                            sortable: true,
                            width:140,
                        },
                        {
                            label: "出仓类型",
                            prop: "outType",
                            type: "select",
                            dicUrl: "/blade-system/dict-biz/dictionary?code=out_type",
                            filterable: true,
                            clearable:false,
                            props: {
                                label: 'dictValue',
                                value: 'dictKey',
                            },
                            overHidden: true,
                            sortable: true,
                            search: true,
                            width:120,
                        },
                        {
                            label: "仓存编号",
                            prop: "stockSummaryId",
                            type: "input",
                            overHidden: true,
                            sortable: true,
                            width:140,
                        },
                        {
                            label: "相关单号",
                            prop: "aboutNo",
                            type: "input",
                            overHidden: true,
                            sortable: true,
                            search: true,
                            width:120,
                        },
                        {
                            label: "相关项",
                            prop: "aboutItem",
                            type: "input",
                            overHidden: true,
                            sortable: true,
                            search: true,
                            width:120,
                        },
                        {
                            label: "物料编号",
                            prop: "materialNo",
                            type: "input",
                            overHidden: true,
                            sortable: true,
                            search: true,
                            width:180,
                        },
                        {
                            label: "物料名称",
                            prop: "materialName",
                            type: "input",
                            overHidden: true,
                            sortable: true,
                            search: true,
                            width:180,
                        },
                        {
                            label: "规格",
                            prop: "specification",
                            type: "input",
                            overHidden: true,
                            sortable: true,
                            width:180,
                        },
                        {
                            label: "单位",
                            prop: "unit",
                            type: "input",
                            overHidden: true,
                            sortable: true,
                            width:180,
                        },
                        {
                            label: "数量",
                            prop: "num",
                            type: "input",
                            overHidden: true,
                            sortable: true,
                            width:180,
                        },
                        {
                            label: "备注",
                            prop: "remark",
                            type: "input",
                            overHidden: true,
                            sortable: true,
                            width:160,
                        },
                        {
                            label: "仓位",
                            prop: "stockId",
                            type: "select",
                            filterable: true,
                            span:8,
                            dicData:[],
                            props: {
                                label: 'stockName',
                                value: 'id',
                            },
                            // allowCreate:true,
                            dataType: 'string',
                            search: true,
                            overHidden: true,
                            sortable: true,
                            width:120,
                        },
                        {
                            label: "供货单位",
                            prop: "fromNo",
                            type: "input",
                            overHidden: true,
                            sortable: true,
                            width:120,
                        },
                        {
                            label: "单位名称",
                            prop: "fromName",
                            type: "input",
                            overHidden: true,
                            sortable: true,
                            width:120,
                        },
                        {
                            label: "操作人员",
                            prop: "createUserName",
                            type: "input",
                            overHidden: true,
                            sortable: true,
                            width:120,
                        },
                        {
                            label: "操作日期",
                            prop: "createTime",
                            type: "input",
                            overHidden: true,
                            sortable: true,
                            width:120,
                        },
                    ]
                },
                data: []
            };
        },
        computed: {
            ...mapGetters(["permission"]),
            permissionList() {
                return {
                    checkPriceBtn: this.validData(this.permission.stock_out_check_price, false),
                    viewBtn: this.validData(this.permission.stock_out_details_view, false),
                };
            },
            ids() {
                let ids = [];
                this.selectionList.forEach(ele => {
                    ids.push(ele.id);
                });
                return ids.join(",");
            }
        },
        mounted() {
            this.init();
        },
        methods: {
            init(){
                selectStockHomeDict().then(res=>{
                    if(res.data.success){
                        const stockId = this.findObject(this.option.column, 'stockId');
                        stockId.dicData = res.data.data;
                    }
                })
            },
            handleExport(){
                NProgress.start();
                this.query.checkPrice = this.permissionList.checkPriceBtn;
                exportBlob(
                    `/blade-anwise/stock/stock_out_item/export?${this.website.tokenHeader
                    }=${getToken()}`, this.query
                ).then(res => {
                    downloadXls(res.data, `出仓明细${this.$dayjs().format('YYYYMMDDHHmmss')}.xlsx`);
                    NProgress.done();
                });
            },
            rowSave(row, done, loading) {
                add(row).then(() => {
                    this.onLoad(this.page);
                    this.$message({
                        type: "success",
                        message: "操作成功!"
                    });
                    done();
                }, error => {
                    loading();
                    window.console.log(error);
                });
            },
            rowUpdate(row, index, done, loading) {
                update(row).then(() => {
                    this.onLoad(this.page);
                    this.$message({
                        type: "success",
                        message: "操作成功!"
                    });
                    done();
                }, error => {
                    loading();
                    window.console.log(error);
                });
            },
            rowDel(row) {
                this.$confirm("确定将选择数据删除?", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                })
                    .then(() => {
                        return remove(row.id);
                    })
                    .then(() => {
                        this.onLoad(this.page);
                        this.$message({
                            type: "success",
                            message: "操作成功!"
                        });
                    });
            },
            handleDelete() {
                if (this.selectionList.length === 0) {
                    this.$message.warning("请选择至少一条数据");
                    return;
                }
                this.$confirm("确定将选择数据删除?", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                })
                    .then(() => {
                        return remove(this.ids);
                    })
                    .then(() => {
                        this.onLoad(this.page);
                        this.$message({
                            type: "success",
                            message: "操作成功!"
                        });
                        this.$refs.crud.toggleSelection();
                    });
            },
            beforeOpen(done, type) {
                if (["edit", "view"].includes(type)) {
                    getDetail(this.form.id).then(res => {
                        this.form = res.data.data;
                    });
                }
                done();
            },
            searchReset() {
                this.query = {};
                this.salesDateRange = [];
                this.search.salesDateStart = '';
                this.search.salesDateEnd = '';
                this.outDateRange = [];
                this.search.outDateStart = '';
                this.search.outDateEnd = '';
                this.onLoad(this.page);
            },
            sortChange(val) {
                console.log(val)
                this.sort = {
                    prop: val.prop,
                    order: val.order
                };
                let order = "";
                if (this.sort.prop && this.sort.order) {
                    if (this.sort.order === "descending") {
                        order = `${this.sort.prop} desc`
                    } else {
                        order = `${this.sort.prop} asc`
                    }
                }
                this.query.orderBy = order;
                this.onLoad(this.page);
            },
            searchChange(params, done) {
                this.query = params;
                this.page.currentPage = 1
                this.onLoad(this.page, params);
                done();
            },
            selectionChange(list) {
                this.selectionList = list;
            },
            selectionClear() {
                this.selectionList = [];
                this.$refs.crud.toggleSelection();
            },
            currentChange(currentPage){
                this.page.currentPage = currentPage;
            },
            sizeChange(pageSize){
                this.page.pageSize = pageSize;
            },
            refreshChange() {
                this.onLoad(this.page, this.query);
            },
            onLoad(page, params = {}) {
                this.loading = true;
                params.aboutNo = this.aboutItem.idNo;
                params.aboutItem = this.aboutItem.itemNo;
                stockOutDetails(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
                    const data = res.data.data;
                    this.page.total = data.total;
                    this.data = data.records;
                    this.loading = false;
                    this.selectionClear();
                });
            },
            handleDateChange(value) {
                if (value) {
                    this.search.salesDateStart = value[0];
                    this.search.salesDateEnd = value[1];
                } else {
                    this.search.salesDateStart = '';
                    this.search.salesDateEnd = '';
                }
            },
            handleOutDateChange(value) {
                if (value) {
                    this.search.outDateStart = value[0];
                    this.search.outDateEnd = value[1];
                } else {
                    this.search.outDateStart = '';
                    this.search.outDateEnd = '';
                }
            },
        }
    };
</script>

<style>
</style>
